package com.sun.module_base.widget

import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.unit.dp

/**
 *@author:sunc
 *@time :2025/7/31 16:23
 *@desc: 描述
 */

@Composable
fun HorizontalDottedProgressBar(color: Color=MaterialTheme.colorScheme.primary) {
//    val color = MaterialTheme.colorScheme.onPrimary

    val transition = rememberInfiniteTransition()
    val state by transition.animateFloat(
        initialValue = 0f, targetValue = 6f, animationSpec = infiniteRepeatable(
            animation = tween(delayMillis = 700, easing = LinearEasing),
            repeatMode = RepeatMode.Reverse
        )
    )
    DrawCanvas(state = state, color = color)

}

@Composable
fun DrawCanvas(state: Float, color: Color) {
    Canvas(
        modifier = Modifier
            .fillMaxWidth()
            .height(30.dp)
    ) {
        val radius = 4.dp.value
        val padding = 6.dp.value

        for (i in 1..5) {
            val paintRadius = if (i - 1 == state.toInt()) {
                radius * 2
            } else {
                radius
            }
            drawCircle(
                radius = paintRadius,
                brush = SolidColor(color),
                center = Offset(
                    x = this.center.x + radius * 2 * (i - 3) + padding * (i - 3),
                    y = this.center.y
                )
            )
        }
    }
}